import { Image, Space, Button } from "antd";

//渲染商品信息
const renderGoodsItem = (record, key) => {
  return (
    record.pickingGoodsSummarySkuVOList.map((v, index) => {
      return (
        <div key={index}>{v[key]}</div>
      )
    })
  )
}

//字段配置列
const columnsConfig = {
	'dataIndex': {
		title: "序号",
		code: "dataIndex",
		lock: true,
		render(v, r, i) {
			return i + 1;
		}
	},
	'skuImage': {
		title: '图片',
		code: 'skuImage',
    render: t =>  <Image style={{ borderRadius: '4px' }} width={64} src={t} />
	},
  'spuCode': {
		title: '款式编码',
		code: 'spuCode',
	},
  'skuCode': {
		title: '商品编码',
		code: 'skuCode',
    render: (t, r) => renderGoodsItem(r, 'skuCode')
	},
  'colorSpecifications': {
		title: '颜色规格',
		code: 'colorSpecifications',
    render: (t, r) => renderGoodsItem(r, 'colorSpecifications')
	},
  'codeQuantity': {
		title: '数量',
		code: 'codeQuantity',
    render: (t, r) => renderGoodsItem(r, 'codeQuantity')
	},
  'skuPrice': {
		title: '采购单价',
		code: 'skuPrice',
    render: (t, r) => renderGoodsItem(r, 'skuPrice')
	},
  'skuQuantity': {
		title: '总数量',
		code: 'skuQuantity',
	},
  'skuName': {
		title: '商品名称',
		code: 'skuName',
	},
  'skuAbbreviation': {
		title: '商品简称',
		code: 'skuAbbreviation',
	},

	//以下是打印记录的字段
	'createTime': {
		title: '打印时间',
		code: 'createTime',
	},
  'supplierName': {
		title: '供应商',
		code: 'supplierName',
	},
	'supplierAreaName': {
		title: '区域编码',
		code: 'supplierAreaName',
	},
	'num': {
		title: '总数',
		code: 'num',
	},
	'createAccount': {
		title: '创建人',
		code: 'createAccount',
	},
	'opera': {
		title: '操作',
		code: 'opera',
	},
}

//打印记录页面操作列
const RenderOpera = ({ record, handleViewDetail, handlePrint }) => {
	return (
		<Space>
			<Button type="link" onClick={() => handleViewDetail(record.id)}>查看详情</Button>
			<Button type="link" onClick={() => handlePrint(record.id)}>打印备货汇总</Button>
		</Space>
	)
}

export const getCellRenderComp = (modalProps) => {
	const cellRenderConfig = {
		'opera': {
			'render': (v, r) => (<RenderOpera {...{ ...modalProps, record: r, value: v }} />)
		},
	}
	return cellRenderConfig;
}

//获取当前的列函数
export const getColumnsData = (props) => {
	const { columns } = props;
	const arr = [];
	const customRender = getCellRenderComp(props);
	for (let i in columns) {
		const obj = columns[i];
		const item = columnsConfig[obj.code];
		const customRenderData = customRender[obj.code];
		//单独的title渲染和render函数渲染
		if (customRenderData) {
			customRenderData.title && (item.title = customRenderData.title());
			customRenderData.render && (item.render = customRenderData.render);
		}
		if (obj.checked && item) {
			arr.push({
				...item,
				checked: obj.checked,
				width: obj.width,
				lock: obj.lock
			})
		}
	}
	return arr;
}